home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 081 / strip_z.arc / STRIP_Z.C next >
Encoding:
C/C++ Source or Header  |  1987-02-17  |  6.0 KB  |  187 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*               The Opus Computer-Based Conversation System                */
  3. /*       (c) Copyright 1986, Wynn Wagner III, All Rights Reserved           */
  4. /*--------------------------------------------------------------------------*/
  5. /*                                                                          */
  6. /* STRIP_Z: Filter Control-Z (CP/M style end-of-file marker) from text file */
  7. /*                                                                          */
  8. /* COMPILER: MicroSoft C, v4.0                                              */
  9. /*                                                                          */
  10. /*--------------------------------------------------------------------------*/
  11.  
  12. #include <io.h>
  13. #include <stdio.h>
  14. #include <sys\types.h>
  15. #include <sys\stat.h>
  16. #include <malloc.h>
  17.  
  18. #include "legible.h"
  19.  
  20. static char *BUFFER_ERROR = "\r\nFile buffer error. Unable to continue.\r\n$";
  21.  
  22.  
  23.  
  24.  
  25. /*--------------------------------------------------------------------------*/
  26. /* EXTENDED HELP                                                            */
  27. /*--------------------------------------------------------------------------*/
  28. void extended_help()
  29.    begin
  30.        bdos(9,"Strip_Z removes control Z characters from a test file.\r\n$");
  31.        bdos(9,"First, it creates a backup file (`.BAK') of the program...\r\n$");
  32.        bdos(9,"then does the filtering.\r\n\n$");
  33.        bdos(9,"      USAGE:      strip_z filename\r\n\n$");
  34.        bdos(9,"      EXAMPLE:    strip_z c:\\pascal\\files.bbs\r\n\n$");
  35.       exit(1);
  36.    end
  37.  
  38.  
  39. /*--------------------------------------------------------------------------*/
  40. /* STRMFE: Make File Extension                                              */
  41. /*--------------------------------------------------------------------------*/
  42. void strmfe( new, old, extension )
  43.    char *new, *old, *extension;
  44.    begin
  45.       register int i;
  46.  
  47.       strcpy( new, old );
  48.  
  49.       for(i=0;i<79;i++)
  50.          if (!new[i])
  51.             begin
  52.                new[i]   = '.';
  53.                new[i+1] = '\0';
  54.                goto done;
  55.             end
  56.          else if (new[i]=='.')
  57.             begin
  58.                new[i+1] = '\0';
  59.                goto done;
  60.             end
  61. done:
  62.       strcat( new, extension );
  63.    end /* strmfe */
  64.  
  65.  
  66.  
  67.  
  68. /*--------------------------------------------------------------------------*/
  69. /* MAIN: Strip_Z                                                            */
  70. /*--------------------------------------------------------------------------*/
  71. main(argc,argv)
  72.    int   argc;
  73.    char *argv[];
  74.    begin
  75.       register int   c;
  76.  
  77.       FILE          *source;
  78.       FILE          *dest;
  79.  
  80.       char          *source_buffer;
  81.       char          *dest_buffer;
  82.  
  83.       char           backup[82];
  84.       struct stat   *statptr;
  85.  
  86.  
  87.  
  88.       bdos(9,"STRIP_Z[1.0]\r\n$");
  89.       if (argc<2) extended_help();
  90.  
  91.       /*--------------------------------------------------------------------*/
  92.       /* See if the requested file exists.                                  */
  93.       /*--------------------------------------------------------------------*/
  94.       statptr  = (struct stat *)malloc( sizeof(struct stat) );
  95.       if (stat(argv[1],statptr))
  96.          begin
  97.              free( statptr );
  98.             bdos(9,"\r\n\nNO SUCH FILE\r\n$");
  99.             extended_help();
  100.          end
  101.       free( statptr );
  102.  
  103.  
  104.       /*--------------------------------------------------------------------*/
  105.       /* Maintain a backup copy of the file.                                */
  106.       /*--------------------------------------------------------------------*/
  107.       strmfe( backup, argv[1], "Bak" );
  108.       unlink( backup );
  109.       if (rename(argv[1],backup))
  110.          begin
  111.              bdos(9,"\r\n\nCAN'T CREATE BACKUP\r\n$");
  112.             extended_help();
  113.          end
  114.  
  115.  
  116.       /*--------------------------------------------------------------------*/
  117.       /* Open the source and destination files                              */
  118.       /*--------------------------------------------------------------------*/
  119.       source   = fopen( backup, "rb" );
  120.       if (!source)
  121.          begin
  122.              bdos(9,"\r\nCAN'T OPEN SOURCE FILE\r\n$");
  123.             extended_help();
  124.          end
  125.  
  126.       dest     = fopen( argv[1], "wb" );
  127.       if (!dest)
  128.          begin
  129.              bdos(9,"\r\nCAN'T OPEN DESTINATION FILE\r\n$");
  130.             extended_help();
  131.          end
  132.  
  133.  
  134.       /*--------------------------------------------------------------------*/
  135.       /* Setup some elephant buffers                                        */
  136.       /*--------------------------------------------------------------------*/
  137.       c  = _memavl() / 3;
  138.       source_buffer  = malloc(c);
  139.       dest_buffer    = malloc(c);
  140.  
  141.       if ((!source_buffer) or (!dest_buffer))
  142.          begin
  143.              bdos(9,"\r\nMEMORY ERROR: unable to continue\r\n$");
  144.             exit(1);
  145.          end
  146.  
  147.       if (setvbuf(source,source_buffer,_IOFBF,c))
  148.          begin
  149.              bdos(9,BUFFER_ERROR);
  150.             exit(1);
  151.          end
  152.  
  153.       if (setvbuf(dest,dest_buffer,_IOFBF,c))
  154.          begin
  155.              bdos(9,BUFFER_ERROR);
  156.             exit(1);
  157.          end
  158.  
  159.  
  160.  
  161.       /*--------------------------------------------------------------------*/
  162.       /* Copy the file                                                      */
  163.       /*--------------------------------------------------------------------*/
  164.       while(1)
  165.          begin
  166.              switch( c=getc(source))
  167.                 begin
  168.  
  169.                     case   EOF  :  fclose(source);
  170.                                  fclose(dest);
  171.                                  free(source_buffer);
  172.                                  free(dest_buffer);
  173.                                  exit(0);
  174.  
  175.                   case   0x1a :  break;
  176.  
  177.                   default     :  putc( c, dest );
  178.  
  179.                 end /* switch */
  180.          end
  181.  
  182.        
  183.    end
  184.  
  185.  
  186. /* END OF FILE: strip_z.c */
  187.